package com.dd.cloud.user.service.log.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.common.constants.LogConstants;
import com.dd.cloud.user.entity.log.LogError;
import com.dd.cloud.user.mapper.log.LogErrorMapper;
import com.dd.cloud.user.service.log.ILogErrorService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 系统异常日志表, 需要手动处理的异常 服务实现类
 * </p>
 *
 * @author czg
 * @since 2022-04-27
 */
@Service
public class LogErrorServiceImpl extends ServiceImpl<LogErrorMapper, LogError> implements ILogErrorService {
    /**
     * 添加异常日志
     *
     * @param msg
     * @param returnInfo
     * @param params
     */
    @Override
    public void presBackLog(String msg, String returnInfo, String params) {
        if (!StringUtils.isBlank(msg)) {
            msg = msg.length() > 1000 ? msg.substring(0, 999) : msg;
        }
        if (!StringUtils.isBlank(params)) {
            params = params.length() > 500 ? params.substring(0, 499) : params;
        }
        if (!StringUtils.isBlank(returnInfo)) {
            returnInfo = returnInfo.length() > 1000 ? returnInfo.substring(0, 999) : returnInfo;
        }
        LogError logError = new LogError();
        logError.setMsg(msg);
        logError.setReturnInfo(returnInfo);
        logError.setCreateDate(LocalDateTime.now());
        logError.setParams(params);
        logError.setStatus(LogConstants.ErrorStatus.NOMER);
        save(logError);
    }
}
